<template>
  <div>
    <div class="head">
      <div class="title">
        {{ total }} 件商品 {{ ' ' }}
        <div class="active">{{ page }}</div>
        /{{ Math.ceil(total / limit) }}
      </div>

      <div class="switch">
        <div
          class="item pointer"
          @click="type = 0"
          :class="type == 0 ? 'active' : ''"
        >
          <img :src="`@/assets/imgs/grid${type == 0 ? '-active' : ''}.png`" />
          矩阵
        </div>
        <div
          class="item pointer"
          @click="type = 1"
          :class="type == 1 ? 'active' : ''"
        >
          <img :src="`@/assets/imgs/list${type == 1 ? '-active' : ''}.png`" />
          列表
        </div>
      </div>
      <div class="clear" />
    </div>
    <div class="list" v-if="type == 0 && list.length > 0">
      <div class="item" v-for="(item, index) in list" :key="index">
        <div class="pic">
          <img
            :src="item.attrList[item.buyIndex].proPicList[0]"
            @click="goto('../detail/detail?id=' + item.id)"
            v-if="item.attrList.length > 0"
          />
          <img
            src="@/assets/imgs/nopic.png"
            @click="goto('../detail/detail?id=' + item.id)"
            v-else
          />
        </div>
        <div class="name">
          {{ item.name }}
        </div>
        <div class="price">
          {{
            item.attrList.length > 0
              ? '$' + item.attrList[item.buyIndex].promotionPrice
              : '暂无价格'
          }}
        </div>
        <div class="pic-list">
          <img
            :src="oitem.proPicList[0]"
            class="pic-item"
            v-for="(oitem, oindex) in item.attrList"
            :class="oindex == item.buyIndex ? 'active' : ''"
            :key="oindex"
            @click="switchAttr(index, oindex)"
          />
          <div
            class="pic-item dot"
            @click="goto('../detail/detail?id=' + item.id)"
          >
            ···
          </div>
        </div>
        <div class="bottom">
          <div class="number">
            <div
              class="minus"
              :class="item.buyNumber == 1 ? 'disable' : ''"
              @click="
                list[index].buyNumber = Math.max(1, item.buyNumber - 1)
                $forceUpdate()
              "
            >
              -
            </div>
            <input
              type="number"
              :value="item.buyNumber"
              class="total"
              @input="onInput($event, index)"
            />
            <div
              class="plus"
              @click="
                list[index].buyNumber = item.buyNumber + 1
                $forceUpdate()
              "
            >
              +
            </div>
          </div>
          <div
            class="button pointer"
            @click="
              addToCart(
                item.buyNumber,
                item.attrList[item.buyIndex].id,
                item.id
              )
            "
          >
            加入购物车
          </div>
        </div>
      </div>
      <div class="clear" />
    </div>
    <div class="list2" v-if="type == 1">
      <div class="item" v-for="(item, index) in list" :key="index">
        <div class="pic">
          <img
            :src="item.attrList[item.buyIndex].proPicList[0]"
            @click="goto('../detail/detail?id=' + item.id)"
            v-if="item.attrList.length > 0"
          />
          <img
            src="@/assets/imgs/nopic.png"
            @click="goto('../detail/detail?id=' + item.id)"
            v-else
          />
        </div>
        <div class="name">{{ item.name }}</div>
        <div class="attrs" v-if="item.attrList.length > 0">
          <div
            class="attrs-item"
            v-for="(oitem, oindex) in item.attrList[item.buyIndex].productAttr"
            :key="oindex"
          >
            {{ oitem.name }}：{{ oitem.value }}
          </div>
        </div>
        <div class="pic-list">
          <img
            :src="oitem.proPicList[0]"
            class="pic-item"
            v-for="(oitem, oindex) in item.attrList"
            :class="oindex == item.buyIndex ? 'active' : ''"
            :key="oindex"
            @click="switchAttr(index, oindex)"
          />
        </div>
        <div class="price">
          {{
            item.attrList.length > 0
              ? '$' + item.attrList[item.buyIndex].promotionPrice
              : '暂无价格'
          }}
        </div>
        <div class="number">
          <div
            class="minus"
            :class="item.buyNumber == 1 ? 'disable' : ''"
            @click="
              list[index].buyNumber = Math.max(1, item.buyNumber - 1)
              $forceUpdate()
            "
          >
            -
          </div>
          <input
            type="number"
            :value="item.buyNumber"
            class="total"
            @input="onInput($event, index)"
          />
          <div
            class="plus"
            @click="
              list[index].buyNumber = item.buyNumber + 1
              $forceUpdate()
            "
          >
            +
          </div>
        </div>
        <div
          class="button pointer"
          @click="
            this.$parent.addToCart(
              item.buyNumber,
              item.attrList[item.buyIndex].id,
              item.id
            )
          "
        >
          加入购物车
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    olist: {
      type: Array,
      default: []
    },
    total: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      type: 0,
      list: [],
      page: 1,
      limit: 8
    }
  },
  mounted() {
    console.log(this)
    this.list = [...this.olist]
    this.list.map((item, index) => {
      this.list[index].buyNumber = 1
      this.list[index].buyIndex = 0
      this.list[index].showPicIndex = 0
    })
  },
  methods: {
    onInput(e, index) {
      this.list[index].buyNumber = e.detail.value
    },
    switchAttr(index, oindex) {
      this.list[index].buyIndex = oindex
      this.$forceUpdate()
    }
  }
}
</script>

<style lang="scss" scoped>
.head {
  height: 30px;
  line-height: 30px;

  .title {
    font-size: 20px;
    font-weight: 500;
    color: #222222;
    display: inline-block;

    .active {
      color: #ff590f;
      display: inline;
    }
  }

  .switch {
    float: right;

    .item {
      display: flex;
      height: 30px;
      align-items: center;
      float: left;
      width: 82px;
      background-color: #dddddd;
      font-size: 14px;
    }

    .item.active {
      background-color: #ff590f;
      color: #ffffff;
    }

    img {
      height: 14px;
      width: 14px;
      margin-left: 16px;
      margin-right: 8px;
    }
  }
}

.list {
  margin-top: 20px;
  margin-left: -8px;
  margin-right: -8px;

  .item:hover {
    border: 2px solid #ff590f;

    .bottom .button {
      background: #ff590f;
    }
  }

  .item {
    width: 288px;
    height: 484px;
    border: 1px solid #dddddd;
    float: left;
    margin-left: 8px;
    margin-right: 8px;
    margin-bottom: 16px;

    .pic {
      height: 248px;
      width: 248px;
      background-color: white;
      width: 248px;
      text-align: center;
      margin-top: 20px;
      margin-left: 20px;

      img {
        height: 248px;
        width: 248px;
      }
    }

    .name {
      width: 248px;
      height: 56px;
      font-size: 16px;
      font-weight: 600;
      color: #222222;
      line-height: 28px;
      margin-top: 20px;
      margin-left: 20px;
      text-align: center;
      line-height: 28px;
    }

    .price {
      font-size: 18px;
      text-align: center;
      font-weight: 400;
      color: #ff590f;
      margin-top: 8px;
      line-height: 18px;
    }

    .pic-list {
      width: 248px;
      margin-left: 20px;
      margin-top: 15px;
      display: flex;
      align-items: center;

      .pic-item {
        display: inline-block;
        width: 34px;
        height: 34px;
        border: 1px solid #dddddd;
        margin-right: 8px;
      }

      .pic-item.active {
        border: 1px solid #ff590f;
        text-align: center;
      }

      .pic-item.dot {
        margin-right: 0;
        line-height: 34px;
        text-align: center;
        font-size: 24px;
      }
    }

    .bottom {
      width: 248px;
      margin-left: 20px;
      margin-top: 16px;
      display: flex;
      align-items: center;

      .number {
        display: inline-flex;
        align-items: center;

        .minus,
        .plus {
          width: 34px;
          height: 34px;
          border: 1px solid #dddddd;
          text-align: center;
          display: inline-block;
          line-height: 34px;
        }

        .total {
          height: 34px;
          width: 40px;
          line-height: 40px;
          border-top: 1px solid #dddddd;
          border-bottom: 1px solid #dddddd;
          text-align: center;
          display: inline-block;
          font-size: 14px;
          font-weight: 500;
          color: #17183b;
        }

        .disable {
          background-color: #dddddd;
        }
      }

      .button {
        width: 118px;
        height: 34px;
        line-height: 34px;
        background: #535353;
        text-align: center;
        font-size: 14px;
        font-weight: 500;
        color: #ffffff;
        display: inline-block;
        margin-left: 18px;
      }
    }
  }
}

.list2 {
  border-top: 1px solid #dddddd;
  margin-top: 20px;

  .item:hover {
    .pic {
      border: 2px solid #ff590f;
    }

    .button {
      background-color: #ff590f;
    }
  }

  .item {
    height: 204px;
    border-bottom: 1px solid #dddddd;
    position: relative;

    .pic {
      width: 164px;
      height: 164px;
      background: #ffffff;
      border: 1px solid #dddddd;
      position: relative;
      top: 20px;
      display: flex;
      align-items: center;
      justify-content: center;

      img {
        width: 136px;
        height: 156px;
        background-color: white;
      }
    }

    .name {
      font-size: 16px;
      font-weight: 600;
      color: #222222;
      position: absolute;
      top: 24px;
      left: 212px;
      line-height: 1;
    }

    .attrs {
      position: absolute;
      top: 52px;
      left: 212px;
      max-height: 80px;
      overflow-y: hidden;

      .attrs-item {
        font-size: 14px;
        font-weight: 400;
        color: #535353;
        line-height: 20px;
      }
    }

    .pic-list {
      height: 34px;
      position: absolute;
      left: 212px;
      bottom: 20px;

      .pic-item {
        width: 34px;
        height: 34px;
        border: 1px solid #dddddd;
        margin-right: 8px;
      }

      .pic-item.active {
        border: 1px solid #ff590f;
      }
    }

    .price {
      font-size: 18px;
      font-weight: 400;
      color: #222222;
      line-height: 18px;
      position: absolute;
      top: 93px;
      left: 640px;
    }

    .number {
      display: inline-flex;
      align-items: center;
      position: absolute;
      top: 81px;
      left: 790px;

      .minus,
      .plus {
        width: 42px;
        height: 42px;
        border: 1px solid #dddddd;
        text-align: center;
        display: inline-block;
        line-height: 38px;
        font-size: 24px;
      }

      .total {
        height: 42px;
        width: 64px;
        line-height: 40px;
        border-top: 1px solid #dddddd;
        border-bottom: 1px solid #dddddd;
        text-align: center;
        display: inline-block;
        font-size: 16px;
        font-weight: 500;
        color: #17183b;
      }

      .disable {
        background-color: #dddddd;
      }
    }

    .button {
      width: 164px;
      height: 42px;
      background: #535353;
      line-height: 42px;
      text-align: center;
      position: absolute;
      color: #ffffff;
      top: 81px;
      right: 0;
      font-size: 16px;
      font-weight: 500;
    }
  }
}
</style>
